/*
 * @Descripttion: 文件操作
 * @version: 1.0
 * @LastEditors: mh
 * @LastEditTime: 2023年2月16日10:00:41
 */
const file = {}

//文件下载
file.downLoadFile = (res, nickName = 'unknown') => {
	var blob = new Blob([res.data], {
		type: 'application/octet-stream;charset=UTF-8'
	})

	var filename = ''
	var contentDisposition = res.headers['content-disposition']
	if (contentDisposition) {
		var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
		var result = patt.exec(contentDisposition)
		filename = result[1]
	} else {
		filename = nickName
	}
	var downloadElement = document.createElement('a')
	var href = window.URL.createObjectURL(blob) // 创建下载的链接
	var reg = /^["](.*)["]$/g
	downloadElement.style.display = 'none'
	downloadElement.href = href
	downloadElement.download = decodeURI(filename.replace(reg, '$1')) // 下载后文件名
	document.body.appendChild(downloadElement)
	downloadElement.click() // 点击下载
	document.body.removeChild(downloadElement) // 下载完成移除元素
	window.URL.revokeObjectURL(href) // 释放URL 对象
}

export default file
